﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webtracuudiem.Truyxuatdulieu;
using System.Data;

namespace webtracuudiem.Admin
{
    public partial class taotaikhoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnTaoTaiKhoan_Click(object sender, EventArgs e)
        {
            //Ktra du lieu
            if (IsValid == false) //neu 1 trong cac validator kiem tra khong hop le thi IsValid ==false
            {
                return;
            }
            string username = txtUserName.Text;
            string password = txtPassword.Text;
            string hoten=txtHoTen.Text;
            bool gioitinhNam = radNam.Checked;
            string maquyen = DropDownList1.SelectedValue;

            DateTime ngaysinh ;
            if (txtNgaySinh.Text != "")
                ngaysinh = DateTime.ParseExact(txtNgaySinh.Text, "d/M/yyyy", new System.Globalization.CultureInfo("fr-FR"));
            else
                ngaysinh = new DateTime(1900, 1, 1);

            string caulenhSqlInsert = "INSERT INTO NguoiDung([Username],[Password],[HoTen],[GioiTinh],[NgaySinh]) VALUES(@Username,@Password,@HoTen,@GioiTinh,@NgaySinh)";
            //int ketqua=TienIchSql.ChayLenhCapNhatDuLieu(caulenhSQLInsert,new string[] {"@Username","@Password","@HoTen","@GioiTinh","@NgaySinh"},new object[] {username, password,hoten,gioitinhNam,ngaysinh});
            int ketQua = TienIchSql.ChayLenhCapNhatDuLieu(caulenhSqlInsert, "@Username", username, "@Password", password, "@HoTen", hoten, "@GioiTinh", gioitinhNam, "@NgaySinh", ngaysinh);
            //lblThongBao.Text = "So dong duoc them vao la: " + ketQua.ToString();
            caulenhSqlInsert = "INSERT INTO QuyenNguoiDung VALUES('" + maquyen + "','" + username + "',NULL)";
            int ketQua2 = TienIchSql.ChayLenhCapNhatDuLieu(caulenhSqlInsert);

            GridView1.DataBind();
//            Response.Redirect(Request.RawUrl);
        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox5_TextChanged(object sender, EventArgs e)
        {

        }

        protected void UsernameCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string username = txtUserName.Text;
            string sqlSelect = "SELECT * FROM NguoiDung WHERE [Username]=@TenDangNhap";
            //string[] arrTenThamSo =new string[]{"@Username"};
            //object[] arrGiaTriThamSo = new object[]{username};
            //DataTable tblKetQua = TienIchSQL.ChayLenhSelectDuLieu(sqlSelect
            //                                                        , arrTenThamSo
            //                                                        , arrGiaTriThamSo);
            DataTable tblKetQua = TienIchSql.ChayLenhSelectDuLieu(sqlSelect, "@TenDangNhap", username);
            if (tblKetQua.Rows.Count > 0)
                args.IsValid = false;
            else
                args.IsValid = true;

        }

        protected void btnXoaTaiKhoan_Click(object sender, EventArgs e)
        {

            string username = txtUserName.Text;
            string caulenhSql = "DELETE FROM QuyenNguoiDung WHERE USERNAME=@username DELETE FROM NGUOIDUNG WHERE USERNAME=@username";
            int ketQua = TienIchSql.ChayLenhCapNhatDuLieu(caulenhSql,"@username",username);
            GridView1.DataBind();
        }

        protected void btnSuaTaiKhoan_Click(object sender, EventArgs e)
        {

        }

        //protected void Username_TextChange(object sender, EventArgs e)
        //{
        //    string username = txtUserName.Text;
        //    string sqlSelect = "SELECT * FROM NguoiDung WHERE [Username]=@TenDangNhap";
        //    //string[] arrTenThamSo =new string[]{"@Username"};
        //    //object[] arrGiaTriThamSo = new object[]{username};
        //    //DataTable tblKetQua = TienIchSQL.ChayLenhSelectDuLieu(sqlSelect
        //    //                                                        , arrTenThamSo
        //    //                                                        , arrGiaTriThamSo);
        //    DataTable tblKetQua = TienIchSql.ChayLenhSelectDuLieu(sqlSelect, "@TenDangNhap", username);
        //    if (tblKetQua.Rows.Count > 0)
        //        //e.IsValid = false;
        //        //e.Equals=false;
        //        Label1.Text = "Username da ton tai";
        //    else
        //        //e.IsValid = true;
        //        //e.Equals = true;
        //        Label1.Text = "";
        //}
    }
}